package com.zcl.widget;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.view.GestureDetector.OnGestureListener;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.widget.Gallery;


@SuppressLint("HandlerLeak")
public class AdvGallery extends Gallery implements OnGestureListener{

	private int playTime = 3000;
	private ScrollLayout mPager;
	private boolean flag = true;
	
	private Handler mHandler = new Handler(){

		@Override
		public void handleMessage(Message msg) {
			// TODO Auto-generated method stub
			super.handleMessage(msg);
			goNext();
			if(flag){
				mHandler.sendEmptyMessageDelayed(1, playTime);	
			}
		}
		
	};
	public AdvGallery(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		this.setSpacing(1);
		// TODO Auto-generated constructor stub
	}
	
	public AdvGallery(Context context, AttributeSet attrs) {
		super(context, attrs);
		this.setSpacing(1);
		// TODO Auto-generated constructor stub
	}
	
	public AdvGallery(Context context) {
		super(context);
		this.setSpacing(1);
		// TODO Auto-generated constructor stub
	}
	
	public void setPager(ScrollLayout pager)
	{
		mPager = pager;
	}

	@Override
	public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
			float velocityY) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public void setUnselectedAlpha(float unselectedAlpha) {
		// TODO Auto-generated method stub
		unselectedAlpha = 1.0f;
		super.setUnselectedAlpha(unselectedAlpha);
	}
	
	

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		// TODO Auto-generated method stub
		mPager.requestDisallowInterceptTouchEvent(true);
		return super.onTouchEvent(event);
	}

	@Override
	public boolean dispatchKeyEvent(KeyEvent event) {
		// TODO Auto-generated method stub
		mPager.requestDisallowInterceptTouchEvent(true);
		return super.dispatchKeyEvent(event);
	}

	@Override
	public boolean onInterceptTouchEvent(MotionEvent ev) {
		// TODO Auto-generated method stub
		mPager.requestDisallowInterceptTouchEvent(true);
		return super.onInterceptTouchEvent(ev);
	}

	private void goNext()
	{
		this.onScroll(null, null, 1 + 1, 0);
//		this.onKeyDown(KeyEvent.KEYCODE_DPAD_RIGHT, null);
		Method method = null;
		try {
    		method = super.getClass().getSuperclass().getDeclaredMethod("moveNext");
		} catch (SecurityException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NoSuchMethodException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		method.setAccessible(true);
		try {
			method.invoke(this);
		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public void startMove()
	{
		flag = true;
		mHandler.sendEmptyMessageDelayed(1, playTime);
	}
	
	public void stopMove(){
		flag = false;
	}
	
	public boolean getState(){
		return flag;
	}
}
